<?php

/**
 * @author aisyah
 * @copyright 2012
 */

class Paging {
    
    var $batas;
    var $posisi;   
    
    public function cari_posisi($batas){
        if(empty($_GET['halaman'])){
            $posisi = 0;
            $_GET['halaman'] = 1;
        }else{
            $posisi = ($_GET['halaman']-1)*$batas;
        }
        return $posisi;
    }
    
    public function jml_halaman($jml_data, $batas){
        $jml_hal = ceil($jml_data/$batas);
        return $jml_hal;
    }  
        
    function navHalaman($modul,$halaman_aktif, $jmlhalaman){
        $link_halaman = "<div class=paging>";
        
        // Link ke halaman pertama (first) dan sebelumnya (prev)
        if($halaman_aktif > 1){
            
        	$prev = $halaman_aktif-1;
            if($modul==null){
                $link_halaman .= "<span class=prevnext><a href=$_SERVER[PHP_SELF]?halaman=1><< First</a></span>
                            <span class=prevnext><a href=$_SERVER[PHP_SELF]?halaman=$prev>< Prev</a></span>";
            }else{
                $link_halaman .= "<span class=prevnext><a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=1><< First</a></span> 
                            <span class=prevnext><a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$prev>< Prev</a></span>";
            }
        	
        }
        else{ 
        	$link_halaman .= "<span class=disabled><< First</span><span class=disabled>< Prev</span>";
        }
        
        // Link halaman 1,2,3, ...
        $angka = ($halaman_aktif > 5 ? " ... " : " "); 
        for ($i=$halaman_aktif-2; $i<$halaman_aktif; $i++){
          if ($i < 1)
          	continue;
            if(is_null($modul)){
                $angka .= "<a href=$_SERVER[PHP_SELF]?halaman=$i>$i</a>";
            }else{
                $angka .= "<a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$i>$i</a>";
            }
        	  
          }
        	  $angka .= " <span class=current><b>$halaman_aktif</b></span>";
        	  
            for($i=$halaman_aktif+1; $i<($halaman_aktif+3); $i++){
            if($i > $jmlhalaman)
              break;
              if(is_null($modul)){
                $angka .= "<a href=$_SERVER[PHP_SELF]?halaman=$i>$i</a>";
              }else{
                $angka .= "<a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$i>$i</a>";
              }
        	  
            }
            
            if(is_null($modul)){
                $angka .= ($halaman_aktif+2<$jmlhalaman ? " ... <a href=$_SERVER[PHP_SELF]?halaman=$jmlhalaman>$jmlhalaman</a>" : " ");
            }else{
                $angka .= ($halaman_aktif+2<$jmlhalaman ? " ... <a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$jmlhalaman>$jmlhalaman</a>" : " ");
            }
        	  
        
        $link_halaman .= "$angka";
        
        // Link ke halaman berikutnya (Next) dan terakhir (Last) 
        if($halaman_aktif < $jmlhalaman){
        	$next = $halaman_aktif+1;
            if(is_null($modul)){
                $link_halaman .= " <span class=prevnext><a href=$_SERVER[PHP_SELF]?halaman=$next>Next ></a></span> 
                             <span class=prevnext><a href=$_SERVER[PHP_SELF]?halaman=$jmlhalaman>Last >></a></span> ";
            }else{
                	$link_halaman .= " <span class=prevnext><a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$next>Next ></a></span> 
                             <span class=prevnext><a href=$_SERVER[PHP_SELF]?modul=$modul&halaman=$jmlhalaman>Last >></a></span> ";
            }
        
        }
        else{
        	$link_halaman .= " <span class=disabled>Next ></span><span class=disabled>Last >></span>";
        }
        $link_halaman .="</div>";
        return $link_halaman;
    }
}